665B - Shopping - CodeForces Solution


brute force *1400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Sergio cout.tie(NULL);
#define endl "\n";

//Aliases

using ll= long long;
using lld= long double;
using ull= unsigned long long;

//Constants
const lld pi= 3.141592653589793238;
const ll INF= LONG_LONG_MAX;
const ll mod=1e9+7;
const ll MAXN = 1e7;

//TypeDEf
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll,ll> umll;
typedef map<ll,ll> mll;

// Macros
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define fl(i,n) for(int i=0;i<n;i++)
#define rl(i,m,n) for(int i=n;i>=m;i--)
#define py cout<<"YES\n";
#define pm cout<<"-1\n";
#define pn cout<<"NO\n";
#define vr(v) v.begin(),v.end()
#define rv(v) v.end(),v.begin()

//Debug
#ifdef Sergio
#define debug(x) cerr << #x<<" "; cerr<<x<<" "; cerr << endl;
#else
#define debug(x);
#endif
template<typename T1, typename T2> // cin >> pair<T1, T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}
template<typename T1, typename T2> // cout << pair<T1, T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} //__gcd 
ll moduloMultiplication(ll a,ll b,ll mod){ll res = 0;a %= mod;while (b){if (b & 1)res = (res + a) % mod;b >>= 1;}return res;}
ll powermod(ll x, ll y, ll p){ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}
ll mminvprime(ll a, ll b) { return powermod(a, b - 2, b); }
ll mod_add(ll a, ll b, ll m)
{
    a = a % m;
    b = b % m;
    return (((a + b) % m) + m) % m;
}
ll mod_mul(ll a, ll b, ll m)
{
    a = a % m;
    b = b % m;
    return (((a * b) % m) + m) % m;
}
ll mod_sub(ll a, ll b, ll m)
{
    a = a % m;
    b = b % m;
    return (((a - b) % m) + m) % m;
}
ll mod_div(ll a, ll b, ll m)
{
    a = a % m;
    b = b % m;
    return (mod_mul(a, mminvprime(b, m), m) + m) % m;
}

//Graph-dfs
// bool gone[MN];
// vector<int> adj[MN];
// void dfs(int loc){
//     gone[loc]=true;
//     for(auto x:adj[loc])if(!gone[x])dfs(x);
// }

//Sorting
bool sorta(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
bool sortd(const pair<int,int> &a,const pair<int,int> &b){return (a.second > b.second);}

//Bits
string decToBinary(int n){string s="";int i = 0;while (n > 0) {s =to_string(n % 2)+s;n = n / 2;i++;}return s;}
ll binaryToDecimal(string n){string num = n;ll dec_value = 0;int base = 1;int len = num.length();for(int i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}

//Check
bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}
bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}
ll spf[MAXN];
 
    ll SqRoot(ll n){
        double num;
        num=ceil(sqrt(n));
        if(num*num>n) num--;
        num=(ll)num;
        return num;
    }
 
    ll Pow(ll x, ll y, ll mod){
        ll num=1;
        while(y>0){
            if(y%2==1) num=(num*x)%mod;
            y>>=1;
            x=(x*x)%mod;
        }
        return num;
    }
 
    ll Fact(ll n, ll mod){
        ll ans=1;
        for(ll i=2;i<=n;i++){
            ans=((ans%mod)*(i%mod)+mod)%mod;
        }
        ans%=mod;
        return ans;
    }
 
    ll nCr(ll n, ll r, ll mod){
        ll ri=Pow(Fact(r, mod), mod-2, mod);
        ll nri=Pow(Fact(n-r, mod), mod-2, mod);
        ll ans=(((Fact(n, mod)%mod)*(ri%mod))%mod);
        ans=(((ans%mod)*(nri%mod))%mod);
        ans%=mod;
        ans=(ans+mod)%mod;
        return ans;
    }
 
    vector<ll> Prime(ll n){
        vector<ll> vprime;
        vprime.push_back(2);
        vprime.push_back(3);
        vprime.push_back(5);
        for(int i=7; i<=n; i=i+2){
            if(i%5!=0){
                int flag=0;
                for(int j=2; j*j<=i; ++j){
                    if(i%j==0){
                        flag=1;
                        break;
                    }
                }
                if(flag==0){
                    vprime.push_back(i);
                }
            }
        }
        return vprime;
    }
 
    void Factors(ll n, vector<ll> &v){
        for(int i=1; i<=SqRoot(n); ++i){
            if(n%i==0){
                if(n/i==i){
                    v.push_back(i);
                }
                else{
                    v.push_back(i);
                    v.push_back(n/i);
                }
            }
        }
    }
 
    void PrimeFactors(ll n, set<ll> &st){
        while(n%2==0){
            st.insert(2);
            n=n/2;
        }
        for(int i=3; i<=SqRoot(n); i=i+2){
            while(n%i==0){
                st.insert(i);
                n=n/i;
            }
        }
        if(n>2){
            st.insert(n);
        }
    }
 
    void Sieve(){
        spf[1]=1;
        for(int i=2; i<MAXN; ++i){
            spf[i]=i;
        }
        for(int i=4; i<MAXN; i=i+2){
            spf[i]=2;
        }
        for(int i=3; i*i<MAXN; ++i){
            if(spf[i]==i){
                for(int j=i*i; j<MAXN; j=j+i){
                    if(spf[j]==j){
                        spf[j]=i;
                    }
                }
            }
        }
    }
 
    ll lcm(ll a, ll b){
        return (a/__gcd(a, b))*b;
    }
 
    ll revNum(ll num){
        ll myNum=0;
        while(num>0){
            myNum=myNum*10+num%10;
            num=num/10;
        }
        return myNum;
    }
 int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    vll v(k);
    cin>>v;
    ll ans=0;
    while(n--){
        ll j=m;
        while(j--){
            ll x;
            cin>>x;
            ll b=0;
            fl(i,k){
                if(v[i]==x){
                    ans+=i+1;
                    b=i;
                    // debug(i+1);
                    break;
                }
            }
            vll v1(v.begin(),v.end());
            v.clear();
            v.push_back(x);
            for(int i=0;i<k;i++){
                if(v1[i]!=x){
                    v.push_back(v1[i]);
                }
            }
            // cout<<v<<endl;
        }
    }
        cout<<ans<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam